package com.sntk.registermanager.staff.domain.model;

import cn.hutool.core.util.ReUtil;
import com.sntk.registermanager.common.domain.model.AggregateRoot;
import com.sntk.registermanager.common.exception.DataNoLegalException;
import com.sntk.registermanager.common.exception.ObjectDisbaledException;
import lombok.Getter;
import lombok.NonNull;
import org.springframework.util.ObjectUtils;

/**
 * @ClassName: DoctorInfo
 * @author: wgchao
 * @createTime: 2020/11/19 10:00 AM
 * @Description: 医生信息
 */
@Getter
public class DoctorInfo extends AggregateRoot {

    //医生编号
    @NonNull
    private String doctorCode;

    //医生姓名
    @NonNull
    private String doctorName;

    //职称
    private String jobTitle;

    //性别
    private Gender gender;

    //联系电话
    private String phone;

    //医生简介
    private String introduction;

    //备注
    private String remark;

    //照片
    private String imgSrc;

    //禁用状态
    @NonNull
    private Boolean disabled;

    //TODO 对应登陆账户
    private String uname;

    public DoctorInfo(@NonNull Long id, @NonNull String doctorCode, @NonNull String doctorName,
                      String phone, String introduction, String remark,
                      @NonNull Boolean disabled, String jobTitle,
                      @NonNull String uname, @NonNull Gender gender,
                      String imgSrc, @NonNull Long version) {
        setId(id);
        this.doctorCode = doctorCode;
        this.doctorName = doctorName;
        this.phone = phone;
        this.introduction = introduction;
        this.remark = remark;
        this.disabled = disabled;
        this.uname = uname;
        this.jobTitle = jobTitle;
        this.gender = gender;
        this.imgSrc = imgSrc;
        setVersion(version);
    }

    public static DoctorInfo addDoctor(@NonNull String doctorCode, @NonNull String doctorName,
                                       String phone, String introduction, String remark,
                                       String jobTitle, @NonNull Gender gender,
                                       @NonNull String uname, String imgSrc){

        //检查联系电话格式
        if(!ObjectUtils.isEmpty(phone)){
            if(isMatchMobilePhone(phone) || isMatchPhone(phone)){
                throw new DataNoLegalException("医生联系电话格式错误");
            }
        }

        return new DoctorInfo(-1L, doctorCode, doctorName, phone, introduction, remark, false, jobTitle, uname, gender, imgSrc, 0L);
    }

    public void modify(@NonNull String doctorName,
                       String phone, String introduction, String remark,
                       String jobTitle, @NonNull Gender gender){
        normalException();

        if(!ObjectUtils.isEmpty(phone)){
            if(isMatchMobilePhone(phone) || isMatchPhone(phone)){
                throw new DataNoLegalException("医生联系电话格式错误");
            }
        }

        this.doctorName = doctorName;
        this.phone = phone;
        this.introduction = introduction;
        this.remark = remark;
        this.jobTitle = jobTitle;
        this.gender = gender;
    }

    public void disable(){
        this.disabled = true;
    }

    public void enable(){
        this.disabled = false;
    }

    public boolean normal(){
        return !this.disabled;
    }

    public void normalException(){
        if(!normal()){
            throw new ObjectDisbaledException(this.doctorName+"医生已被禁用");
        }
    }

    /**
     * 匹配手机号
     * @return
     */
    private static boolean isMatchMobilePhone(String phoneNumber){
        String regex = "/^1[3456789]d{9}$/";
        return ReUtil.isMatch(regex, phoneNumber);
    }

    /**
     * 匹配固话
     * @return
     */
    private static boolean isMatchPhone(String phoneNumber){
        String regex = "/^((d{3,4})|d{3,4}-|s)?d{7,14}$/";
        return ReUtil.isMatch(regex, phoneNumber);
    }
}
